package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.LikeVo;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Article;
import org.rcloud.io.model.Likes;
import org.rcloud.io.model.LikesVo;
import org.rcloud.io.service.ArticleService;
import org.rcloud.io.service.LikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2019/2/26.
 */
@RestController
@RequestMapping(value = "/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private LikesService likesService;

    @RequestMapping("/findNo")
    @ResponseBody
    @CrossOrigin
    public PageVo<Article> findRuralNoBinding(Integer rId) {
        return articleService.findRuralNoBinding(rId);
    }

    @RequestMapping("/findYes")
    @ResponseBody
    @CrossOrigin
    public PageVo<Article> findRuralYesBinding(Integer rId) {
        return articleService.findRuralYesBinding(rId);
    }

    @RequestMapping("/findSpecialtyNo")
    @ResponseBody
    @CrossOrigin
    public PageVo<Article> findSpecialtyNoBinding(Integer bId) {
        return articleService.findSpecialtyNoBinding(bId);
    }

    @RequestMapping("/findSpecialtyYes")
    @ResponseBody
    @CrossOrigin
    public PageVo<Article> findSpecialtyYesBinding(Integer bId) {
        return articleService.findSpecialtyYesBinding(bId);
    }

    /*
    * 分页查询--摄影--文章的所有信息以及每篇文章的点赞数并排序
    *
    * */
    @RequestMapping(value = "/selectAll",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageInfo<Article> selectAll(@RequestBody String params) {
        JSONObject obj = JSON.parseObject(params);
        Map map = JSON.parseObject(obj.get("params").toString());
        int pageNum = Integer.parseInt(obj.get("pageNum").toString());//页数
        int pageSize = Integer.parseInt(obj.get("pageSize").toString());//查多少条
        PageInfo<Article> PageInfo = articleService.selectAll(pageNum, pageSize, map);
        return PageInfo;
    }

    /*
   * 文章投票
   *
   * */
    @ApiOperation(value="文章投票", notes="文章投票")
    @RequestMapping(value = "/add",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public int add(@RequestBody String params) {
        LikesVo likes = JSONObject.parseObject(params.toString(), LikesVo.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        String s = format + " 00:00:00";
        String s1 = format + " 23:59:59";
        likes.setDate(s);
        likes.setDate1(s1);

        String s2 = "2019-05-23";
        String s3 = "2019-05-28";
        long time = (new Date()).getTime();
        try {
            if (simpleDateFormat.parse(s2).getTime() < time && simpleDateFormat.parse(s3).getTime() > time) {
                Likes likes1 = null;
                       likes1 =  (Likes) likesService.selectByVote(likes).getInfo();
                if (null!=likes1 || likes.equals("")) {
                    System.out.println("=======不能重复投票======");
                    return 2;
                } else {
                    System.out.println("=======未投票，进行投票...======");
                    JSONObject obj = JSON.parseObject(params);
                    obj.put("id", UUID.randomUUID());
                    obj.put("createtime",new Date());
                    obj.put("flag",1);
                    Likes likes2 = JSONObject.parseObject(obj.toString(),Likes.class);
                    return Integer.valueOf(likesService.insertSelective(likes2).getInfo().toString());
                }
            } else {
                System.out.println("未到投票时间");
                return 3;
            }

        } catch (ParseException e) {
            e.printStackTrace();
            return 4;
        }
    }

    /*
    * 查询所有文章对应的投票数以及文章的标题、ID、content
    *
    * */
    @ApiOperation(value="所有文章的投票数", notes="所有文章的投票数")
    @RequestMapping(value = "/selectVoteNum",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin

//  不分页
    public PageVo<Article> selectVoteNum(@RequestBody String params) {
        JSONObject jsonObject = JSONObject.parseObject(params);
        return articleService.selectVoteNum(jsonObject);
    }

//    分页
//    public PageInfo<Article> selectVoteNum(@RequestBody String params) {
//        JSONObject object = JSONObject.parseObject(params.toString());
//        Map map = JSONObject.parseObject(object.get("params").toString());
//        int pageNum = Integer.parseInt(object.get("pageNum").toString());
//        int pageSize = Integer.parseInt(object.get("pageSize").toString());
//        PageInfo<Article> pageInfo = articleService.selectVoteNum(pageNum,pageSize,map);
//        return pageInfo;
//    }


    /**
     *
     * @return
     *      比赛下作品根据点赞数量降序展示
     */
    @RequestMapping(value = "/selectsum",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo selectAllByLike(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer categoryId = (Integer) object.get("categoryId");
        Integer pageNum = (Integer) object.get("pageNum");
        Integer pageSize = (Integer) object.get("pageSize");
        return articleService.selectLikeSum(pageNum,pageSize,categoryId);
    }


    /**
     *
     * @return
     *      比赛下作品根据点赞数量降序展示
     */
    @RequestMapping(value = "/selectsum1")
    @ResponseBody
    @CrossOrigin
    public ResultVo selectAllByLike1(Integer categoryId,Integer pageNum,Integer pageSize) {
        return articleService.selectLikeSum(pageNum,pageSize,categoryId);
    }



    /*
    * 保存文章信息
    * */
   /* @ApiOperation(value="保存文章信息", notes="保存文章信息")
    @ResponseBody
    @RequestMapping(value = "/saveArticle", produces = {"application/json;charset=UTF-8"})
    public int saveArticle(@RequestBody JSONObject params){
        System.out.println("++++++"+params+"====");

        //String title = JSON.parseObject(params).getString("name");

        String title = params.getString("name");
        Article article = new Article();
        article.setTitle(title);
        System.out.println("------"+title+"====");

        return articleService.save(article);

    }*/

    @ApiOperation(value="根据类别id查询相关文章", notes="根据类别id查询相关文章")
    @RequestMapping(value = "/selectbycategoryid",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo selectByCategoryId(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pageNum = (Integer) object.get("pageNum");
        Integer pageSize = (Integer) object.get("pageSize");
        Integer categoryId = (Integer) object.get("categoryId");
        return articleService.selectByCategoryId(pageNum,pageSize,categoryId);
    }

    @ApiOperation(value="查询个人点赞过的作品", notes="查询个人点赞过的作品")
    @RequestMapping(value = "/selectbyuserid",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo selectByUserId(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pageNum = (Integer) object.get("pageNum");
        Integer pageSize = (Integer) object.get("pageSize");
        Integer id = (Integer) object.get("id");
        return articleService.selectByUserId(pageNum,pageSize,id);
    }

    @ApiOperation(value="根据图片查询作者，以及拥有作品", notes="根据图片查询作者，以及拥有作品")
    @RequestMapping(value = "/selectbyarticleid",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo selectByArticleId(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer articleId = (Integer) object.get("articleId");
        return articleService.selectAllByArticleId(articleId);
    }

    @ApiOperation(value="根据openid查询作者以及拥有作品", notes="根据openid查询作者以及拥有作品")
    @RequestMapping(value = "/selectbyopenid",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public ResultVo selectByOpenId(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        String openId = (String) object.get("openId");
        return articleService.selectAllByOpenId(openId);
    }

}
